package com.example.libo.huihui.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v4.util.Pair;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.libo.huihui.R;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Created by libo on 2017/1/10.
 *
 * 抽象的recyclerAdapter
 */

public class BindAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder>{  //泛型为T类型，不能为Object类型
    /**
     * 泛型类型，键为实体类Class类型，链接值为int型的layoutId，所以可以根据当前的实体对象选择对应的布局文件和variableId
     */
    private Map<Class <?>,Pair<Integer,Integer>> map;
    private List<T> list; //因为list里面可以放任何类型数据，所以使用object
    private Context context;
    private ILoadMore loadCallBack;
    private OnItemClickListener onItemClickListener;

    public BindAdapter(Map<Class <?>,Pair<Integer,Integer>> map, List<T> list, Context context) {
        this.map = map;
        this.list = list;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { //viewType就是布局Id
        if(viewType == R.layout.recycler_bottom){
            View view = LayoutInflater.from(context).inflate(R.layout.recycler_bottom,parent,false);
            return new LoadMoreVH(view);
        }else {
            View view = DataBindingUtil.inflate(LayoutInflater.from(context), viewType, parent, false).getRoot();
            ViewDataBinding binding = DataBindingUtil.bind(view);
            return new BindingViewHolder(binding);
        }

    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if(holder instanceof BindingViewHolder){
            Object obj = list.get(position);
            int variableId = map.get(obj.getClass()).second;
            ((BindAdapter.BindingViewHolder)holder).binding.setVariable(variableId,obj);

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClickListener.onItemClick(holder.itemView,position);
                }
            });
        }else{
            if(null != loadCallBack){
                loadCallBack.loadMore();
            }

        }

    }

    @Override
    public int getItemCount() {
        return list.size() + 1;
    }

//    /**
//     * 插入数据
//     * @param obj
//     */
//    public void add(T obj){
//        int size = list.size();
//        list.add(obj);
//        notifyItemInserted(size);
//    }
//
//    public void addAll(Collection<? extends T> collection){
//        int size = list.size();
//        list.addAll(collection);
//        notifyItemRangeInserted(size,collection.size());
//    }

    @Override
    public int getItemViewType(int position) {   //需要多布局，所以需要获取item的layoutID

        if(position == list.size()){
            return R.layout.recycler_bottom;
        }else{
            return map.get(list.get(position).getClass()).first;  //根据位置，返回相对应的布局ID
        }
    }

    public static class BindingViewHolder extends RecyclerView.ViewHolder{
        private ViewDataBinding binding;
        public BindingViewHolder(ViewDataBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    public static class LoadMoreVH extends RecyclerView.ViewHolder {

        public LoadMoreVH(View itemView) {
            super(itemView);
        }
    }

    public void setCallBack(ILoadMore loadCallBack){
        this.loadCallBack = loadCallBack;
    }

    public interface ILoadMore{
        void loadMore();
    }

    public interface OnItemClickListener{
        void onItemClick(View itemView,int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

}
